package com.kehutong.tag.admin;

import com.kehutong.common.entity.Root;
import com.kehutong.crm.enums.TagSourceType;
import com.kehutong.tag.entity.OpinionTag;
import com.kehutong.tag.entity.UserTag;
import com.kehutong.tag.service.OpinionTagService;
import com.kehutong.tag.service.UserTagService;
import org.coraframework.authz.HasPermission;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.util.Objects;
import org.coraframework.util.UUIDGenerator;

import java.io.IOException;
import java.util.List;

/**
 * 舆情标签
 * @author: liukunlong
 * @date: 2021-07-28 11:52
 */
@MvcAction
@ReqMapping("/admin/crm/opinion/tag")
public class OpinionTagController {
    static final Logger logger = LoggerFactory.getLogger(OpinionTagService.class);


    @Inject
    private JdbcSession jdbcSession;

    @Inject
    private OpinionTagService opinionTagService;


    /**
     * 根据条件获取标签列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("crm:opinion:tag:view")
    @ReqMapping("/page")
    public Page<OpinionTag> page(JSONObject jsonObject) throws Exception {
        return opinionTagService.page(jsonObject);
    }

    @HasPermission("crm:opinion:tag:view")
    @ReqMapping("/get")
    public Object get(OpinionTag opinionTagGroup) {
        return opinionTagService.get(opinionTagGroup);
    }


    @HasPermission("crm:opinion:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/save")
    public Object save(OpinionTag opinionTag) {
        return opinionTagService.save(opinionTag);
    }

    @HasPermission("crm:opinion:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/update")
    public Object update(OpinionTag opinionTag) throws Exception {
        return opinionTagService.update(opinionTag);
    }

    @HasPermission("crm:opinion:tag:edit")
    @ReqMapping("/delete")
    public Object delete(OpinionTag opinionTag) {
        return opinionTagService.delete(opinionTag);
    }

    /**
     * 根据条件获取标签组列表
     *
     * @param jsonObject 查询对象
     * @return 住户汽车列表
     * @throws IOException 操作异常
     */
    @HasPermission("crm:opinion:tag:view")
    @ReqMapping("/group/page")
    public Page<OpinionTag> groupPage(JSONObject jsonObject) throws Exception {
        return opinionTagService.groupPage(jsonObject);
    }

    @HasPermission("crm:opinion:tag:view")
    @ReqMapping("/group/list")
    public Object groupList(JSONObject jsonObject) throws Exception {
        return opinionTagService.groupList(jsonObject);
    }

    @HasPermission("crm:opinion:tag:view")
    @ReqMapping("/group/get")
    public Object groupGet(OpinionTag opinionTagGroup) {
        return opinionTagService.groupGet(opinionTagGroup);
    }

    @HasPermission("crm:opinion:tag:edit")
    @Bean(newInstance = true, copy = true)
    @ReqMapping("/group/save")
    public Object groupSave(JSONObject jsonObject) {
        String name=jsonObject.getString("name");
        JSONArray jsonArray=jsonObject.getJSONArray("childList");
        OpinionTag opinionTagGroup = Pool.newInstance(OpinionTag.class, UUIDGenerator.randomUUID());
        opinionTagGroup.setPid(Root.ROOT_ID);
        opinionTagGroup.setGroup(true);
        opinionTagGroup.setName(name);
        opinionTagGroup.setCreateBy(Root.ROOT_ID);
        opinionTagGroup.setCreateName("系统");
        opinionTagGroup.setSort(1);
        List<OpinionTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), OpinionTag.class);
        opinionTagGroup.setChildList(childList);
        return opinionTagService.groupSave(opinionTagGroup);
    }

    @HasPermission("crm:opinion:tag:edit")
    @Bean(copy = true)
    @ReqMapping("/group/update")
    public Object groupUpdate(JSONObject jsonObject) throws Exception {
        JSONArray jsonArray = jsonObject.getJSONArray("childList");
        if(Objects.nonEmpty(jsonArray)){
            for (Object tagJsonObj : jsonArray) {
                JSONObject tagJson = (JSONObject) tagJsonObj;
                tagJson.remove("type");
            }
        }
        jsonObject.remove("childList");
        jsonObject.remove("type");
        OpinionTag opinionTagGroup=JSONObject.parseObject(jsonObject.toJSONString(),OpinionTag.class);
        List<OpinionTag> childList = JSONObject.parseArray(jsonArray.toJSONString(), OpinionTag.class);
        opinionTagGroup.setChildList(childList);
        return opinionTagService.groupUpdate(opinionTagGroup);
    }

    @HasPermission("crm:opinion:tag:edit")
    @ReqMapping("/group/delete")
    public Object groupDelete(OpinionTag opinionTagGroup) {
        return opinionTagService.groupDelete(opinionTagGroup);
    }
}
